package aichat

import (
	"context"
	"encoding/json"
	"fmt"
	db "goweb/db"
	"io"
	"net/http"
	"os"
	"strings"

	"github.com/baidubce/bce-qianfan-sdk/go/qianfan"
)
const API_KEY = "SxiCMqvsVMO5ClaZYFUNzEev"
const SECRET_KEY = "hteXzUl8d8UC8178yu5KcIckEfRz55kM"

func GetAccessToken() string {
    url := "https://aip.baidubce.com/oauth/2.0/token"
    postData := fmt.Sprintf("grant_type=client_credentials&client_id=%s&client_secret=%s", API_KEY, SECRET_KEY)
    resp, err := http.Post(url, "application/x-www-form-urlencoded",  strings.NewReader(postData))
    if err != nil {
        fmt.Println(err)
        return ""
    }
    defer resp.Body.Close()
    body, err := io.ReadAll(resp.Body)
    if err != nil {
        fmt.Println(err)
        return ""
    }
    accessTokenObj := map[string]any{}
    _ = json.Unmarshal([]byte(body), &accessTokenObj)
    return accessTokenObj["access_token"].(string)
}  

func Ai_reply_baidu(question string )string{
    db.Changes_channel <- db.Info_Json{Online: 0, Offline: 0, Warning: 0, Notification: 0, AI: 1, LostNum: 0,TextToSpeech:0,LoginNum:0}
	os.Setenv("QIANFAN_ACCESS_KEY", "ALTAKpSEeYrEyX9BPmIA5u2N72")
	os.Setenv("QIANFAN_SECRET_KEY", "b43cbdac9ae34f99954298c46a1c7574")
    // 指定特定模型
	chat := qianfan.NewChatCompletion(
		qianfan.WithModel("ERNIE-Speed-128K"),
	)
	resp, _ := chat.Do(
		context.TODO(),
		&qianfan.ChatCompletionRequest{
			Messages: []qianfan.ChatCompletionMessage{
				qianfan.ChatCompletionUserMessage(question),

			},
		},
	)
	reply:=resp.Result
	return reply
}

func Ai_reply_xunfei(question string )string{
    db.Changes_channel <- db.Info_Json{Online: 0, Offline: 0, Warning: 0, Notification: 0, AI: 1, LostNum: 0,TextToSpeech:0,LoginNum:0}
	reply:="你说的是："+question+"吗？我不太明白，能不能再详细点？"
	return reply
}


func Text_to_auduio(text string) ([]byte,error) {
    db.Changes_channel <- db.Info_Json{Online: 0, Offline: 0, Warning: 0, Notification: 0, AI: 0, LostNum: 0,TextToSpeech:1,LoginNum:0}
	url := "https://tsn.baidu.com/text2audio"
    // payload := strings.NewReader("tex=%E7%9B%B8%E5%9B%BE%E6%98%AF%E4%B8%80%E7%A7%8D%E7%94%A8%E4%BA%8E%E6%8F%8F%E8%BF%B0%E7%89%A9%E8%B4%A8%E7%B3%BB%E7%BB%9F%E5%9C%A8%E5%A4%9A%E9%87%8D%E7%9B%B8%E4%B9%8B%E9%97%B4%E7%9A%84%E5%B9%B3%E8%A1%A1%E7%8A%B6%E6%80%81%E4%BB%A5%E5%8F%8A%E8%BF%99%E4%BA%9B%E7%8A%B6%E6%80%81%E9%9A%8F%E6%B8%A9%E5%BA%A6%E3%80%81%E5%8E%8B%E5%8A%9B%E6%88%96%E6%88%90%E5%88%86%E5%8F%98%E5%8C%96%E7%9A%84%E5%9B%BE%E7%A4%BA%E6%88%96%E5%9B%BE%E8%A1%A8%E3%80%82&tok=" + GetAccessToken() + "&cuid=pU8MVLmYJqWcvzQ9gB0UmA0Xhgo2yqTw&ctp=1&lan=zh&spd=5&pit=5&vol=5&per=1&aue=3")
    payload := strings.NewReader("tex="+text+"&tok=" + GetAccessToken() + "&cuid=pU8MVLmYJqWcvzQ9gB0UmA0Xhgo2yqTw&ctp=1&lan=zh&spd=5&pit=5&vol=5&per=4119&aue=3")
    client := &http.Client {}
    req, err := http.NewRequest("POST", url, payload)

    if err != nil {
        fmt.Println(err)
        return []byte{},err
    }
    req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
    req.Header.Add("Accept", "*/*")

    res, err := client.Do(req)
    if err != nil {
        fmt.Println(err)
        return []byte{},err
    }
    defer res.Body.Close()

    body, err := io.ReadAll(res.Body)
    if err != nil {
        fmt.Println(err)
        return []byte{},err
    }
	return body,nil
}